class KeypairsController < ApplicationController
  layout nil
  
  def index
    respond_to do |format|
      format.html {
        render
      }
      
      format.json {
        keypairs = []
        Ec2Factory.create(User.find(session[:current_user_id]), session[:url]).describe_key_pairs.each_with_index{|rawKeypair, i|
          p rawKeypair
          
          keypairs << {:id => i + 1, :cell => [
            rawKeypair[:aws_key_name], 
            rawKeypair[:aws_fingerprint],
          ]}
        }
        
        render :json => {
          :page => params[:page],
          :total => keypairs.length / params[:rows].to_i + 1,
          :records => keypairs.length,
          :rows => keypairs 
        }.to_json
      }
    end
  end
  
  def name
    respond_to do |format|
      format.json {
        names = []
        Ec2Factory.create(User.find(session[:current_user_id]), session[:url]).describe_key_pairs.each{|rawKeypair|
          p rawKeypair
          
          names << rawKeypair[:aws_key_name]
        }
        
        render :json => names.to_json
      }
    end
  end
end
